package com.hejia.bulbul.scheduler;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import com.drongam.hermes.entity.SMS;
import com.hejia.alauda.service.BonusService;
import com.hejia.alauda.service.UserService;

/**
 * 用户生日发放红包作业
 * 
 * @author hyr
 *
 */
public class BirthdayScheduler {

	private static final Log logger = LogFactory.getLog(BirthdayScheduler.class);
	
	@Resource
	private UserService userService;
	
	@Resource
	private BonusService bonusService;
	
	/**
	 * 用户生日发放20元红包
	 */
	// [秒] [分] [小时] [日] [月] [周] [年]
	@Scheduled(cron = "0 0 9 * * ?")
	@Async("executor")
	public void birthdayBonus() {
		
		try {
			Date birthday = new Date();
			List<Long> userIds = this.userService.findUserIdsByBirthday(birthday);
			for (Long userId : userIds) {
				try {
					
					this.bonusService.sendBirthdayBonus(userId, birthday);
				} catch (Exception e) {
					logger.error("发送用户生日红包错误，用户Id:" + userId, e);
				}
			}
		} catch (Exception e) {
			logger.error("发放用户生日红包错误", e);
		}
	}
}
